const phoneStr=/^(13[0-9]|15[0-35-9]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const emailStr=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
const idStr=/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
new Vue({
    el:"#regist",
    data:{
        userName:'',
        loginName:'',
        password:'',
        rpassword:'',
        email:'',
        mobile:'',
        identityCode:'',
        validateNum:'',
        isChecked:false,
        infoFlag:false,
        infoText:'',
    },
    methods:{
        register()
        {
            if(this.userName==='')
            {
                this.infoFlag=true
                this.infoText="请填写用户名"
                return
            }
            if(this.loginName==='')
            {
                this.infoFlag=true
                this.infoText="请填写登录名"
                return
            }
            if(this.password==='')
            {
                this.infoFlag=true
                this.infoText="请输入密码"
                return
            }
            if(this.password.length<6)
            {
                this.infoFlag=true
                this.infoText="密码长度至少6位"
                return
            }
            if(this.rpassword!==this.password)
            {
                this.infoFlag=true
                this.infoText="密码确认失败"
                return
            }
            if(this.email==='')
            {
                this.infoFlag=true
                this.infoText="请填写邮箱"
                return
            }
            if(!emailStr.test(this.email))
            {
                this.infoFlag=true
                this.infoText="邮箱格式不正确（xx@ss.ss）"
                return
            }
            if(this.mobile==='')
            {
                this.infoFlag=true
                this.infoText="请填写手机号"
                return
            }
            if(!phoneStr.test(this.mobile))
            {
                this.infoFlag=true
                this.infoText="手机号格式不正确（13 | 15 | 17 | 18 | 19开头）"
                return
            }
            if(this.identityCode==='')
            {
                this.infoFlag=true
                this.infoText="请填写身份证"
                return
            }
            if(!idStr.test(this.identityCode))
            {
                this.infoFlag=true
                this.infoText="身份证格式不正确"
                return
            }
            if(this.validateNum==='')
            {
                this.infoFlag=true
                this.infoText="请输入验证码"
                return
            }
            if(this.isChecked===false)
            {
                this.infoFlag=true
                this.infoText="请勾选用户协议"
                return
            }
            axios.post('/api/doRegister',
                {
                    userName:this.userName,
                    loginName:this.loginName,
                    password:this.password,
                    email:this.email,
                    mobile:this.mobile,
                    identityCode:this.identityCode,
                    validateNum:this.validateNum
                }).then(rs=>{
                    this.infoFlag=true
                    if(rs.data.validateInfo==="no")
                    {
                        this.infoText="验证码无效"
                    }
                    if(rs.data.validateInfo==="inCorrect")
                    {
                        this.infoText="验证码错误"
                    }
                    if(rs.data.validateInfo==="success")
                    {
                        this.infoText="注册成功"
                        setTimeout(()=>{
                            window.location="/api/login.html"
                        },1000)
                    }
                    if(rs.data.validateInfo==="fail")
                    {
                        this.infoText="注册出错了"
                    }

            })

        },

        //验证码计时
        timeDeduce()
        {
            if(this.email==='')
            {
                this.infoFlag=true
                this.infoText="请填写邮箱"
                return false;
            }
            //发送发邮箱的请求
            axios.get('/api/getValidateNum',
                {
                    params:{
                        email:this.email
                    }
                }).then(rs=>{
                    this.infoFlag=true
                    if(rs.data.isSuccess==="yes")
                    {
                        this.infoText="验证码已发送，请注意查收"
                    }else{
                        this.infoText="验证码发送失败，请稍后再发"
                    }
            })
            // this.$refs.timeLeave.disabled=true
            //计时器
            let num=60
            let n=setInterval(()=>{
                num-=1
                this.$refs.timeLeave.value=num+" 秒后重发"

                if(num==0)
                {
                    clearInterval(n)
                    // this.$refs.timeLeave.disabled=false
                    this.$refs.timeLeave.value="获取验证码"
                }
            },1000)

        },
        checkLoginName()
        {
            axios.get('/api/checkLoginName',{
                params:{
                    loginName:this.loginName
                }
            }).then(rs=>{
                if(rs.data.isRepeat==="yes")
                {
                    this.$refs.lg_name.innerHTML="用户名重复"
                    this.$refs.lg_name.style.color="red"
                    this.$refs.register.disabled=true
                }else{
                    this.$refs.lg_name.innerHTML="用户名可用"
                    this.$refs.lg_name.style.color="green"
                    this.$refs.register.disabled=false
                }
            })
        },
        closeInfo()
        {
            this.infoFlag=false
        }
    },

})